<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="公益券码" prop="code">
        <el-input v-model="formData.code" placeholder="请输入公益券码" :disabled="true" />
      </el-form-item>
      <el-form-item label="券码状态" prop="status">
        <el-select
          placeholder="请选择券码状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PROMOTION_COUPON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户手机号" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入用户手机号" />
      </el-form-item>
      <el-form-item label="领取类型" prop="takeType">
        <el-select v-model="formData.takeType" placeholder="请选择领取类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="生效开始时间" prop="validStartTime">
        <el-date-picker
          v-model="formData.validStartTime"
          type="date"
          value-format="x"
          placeholder="选择生效开始时间"
        />
      </el-form-item>
      <el-form-item label="生效结束时间" prop="validEndTime">
        <el-date-picker
          v-model="formData.validEndTime"
          type="date"
          value-format="x"
          placeholder="选择生效结束时间"
        />
      </el-form-item>
      <el-form-item label="券码业务类型" prop="discountType">
        <el-select v-model="formData.discountType" placeholder="请选择券码业务类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="使用订单号" prop="useOrderId">
        <el-input v-model="formData.useOrderId" placeholder="请输入使用订单号" />
      </el-form-item>
      <el-form-item label="使用时间" prop="useTime">
        <el-date-picker
          v-model="formData.useTime"
          type="date"
          value-format="x"
          placeholder="选择使用时间"
        />
      </el-form-item>
      <el-form-item label="券码核销时间" prop="codeRedemptionTime">
        <el-date-picker
          v-model="formData.codeRedemptionTime"
          type="date"
          value-format="x"
          placeholder="选择券码核销时间"
        />
      </el-form-item>
      <el-form-item label="券码核销人" prop="codeRedemptionUser">
        <el-input v-model="formData.codeRedemptionUser" placeholder="请输入券码核销人" />
      </el-form-item>
      <el-form-item label="券码核销地址" prop="codeRedemptionAddr">
        <el-input v-model="formData.codeRedemptionAddr" placeholder="请输入券码核销地址" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CouponCodeApi, CouponCode } from '@/api/mcm/couponcode'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
/** 劵码记录 表单 */
defineOptions({ name: 'CouponCodeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  code: undefined,
  status: undefined,
  mobile: undefined,
  takeType: undefined,
  usePrice: undefined,
  validStartTime: undefined,
  validEndTime: undefined,
  discountType: undefined,
  discountPercent: undefined,
  discountPrice: undefined,
  discountLimitPrice: undefined,
  useOrderId: undefined,
  useTime: undefined,
  codeRedemptionTime: undefined,
  codeRedemptionUser: undefined,
  codeRedemptionAddr: undefined,
})
const formRules = reactive({
  code: [{ required: true, message: '公益券码不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '券码状态', trigger: 'blur' }],
  mobile: [{ required: true, message: '用户手机号不能为空', trigger: 'blur' }],
  takeType: [{ required: true, message: '领取类型', trigger: 'change' }],
  validStartTime: [{ required: true, message: '生效开始时间不能为空', trigger: 'blur' }],
  validEndTime: [{ required: true, message: '生效结束时间不能为空', trigger: 'blur' }],
  discountType: [{ required: true, message: '券码业务类型不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CouponCodeApi.getCouponCode(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CouponCode
    if (formType.value === 'create') {
      await CouponCodeApi.createCouponCode(data)
      message.success(t('common.createSuccess'))
    } else {
      await CouponCodeApi.updateCouponCode(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    code: undefined,
    status: undefined,
    mobile: undefined,
    takeType: undefined,
    usePrice: undefined,
    validStartTime: undefined,
    validEndTime: undefined,
    discountType: undefined,
    discountPercent: undefined,
    discountPrice: undefined,
    discountLimitPrice: undefined,
    useOrderId: undefined,
    useTime: undefined,
    codeRedemptionTime: undefined,
    codeRedemptionUser: undefined,
    codeRedemptionAddr: undefined,
  }
  formRef.value?.resetFields()
}
</script>
